home *** CD-ROM | disk | FTP | other *** search
- /*
- File: IsUserLoggedIn.c
-
- Description: This program demonstrates how to determine if a console
- user is currently logged in.
- Also, the sample demonstrates how to obtain the username
- of the current console user.
-
- Author: Chad Jones (chadj@apple.com)
-
- Copyright: © Copyright 2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Change History (most recent first):
- */
-
- #include <CoreFoundation/CoreFoundation.h>
- #include <SystemConfiguration/SystemConfiguration.h>
-
- /* CopyCurrentConsoleUsername
- *
- * This function will return the username of the
- * console user who is currently logged in. Alternatively
- * this function will return NULL if no user is currently
- * logged into the system.
- */
- CFStringRef CopyCurrentConsoleUsername()
- {
- CFStringRef consoleUserName;
- uid_t uid;
- gid_t gid;
-
- /* Getting the username of current consoleuser. Note
- * that if the username is NULL then that means that
- * no user is presently logged in. We do the lookup
- * using the SCDynamicStoreCopyConsoleUser call.
- * First Argument: The dynamic store to use to lookup
- * the console username. We only need a temporary dynamic
- * store so pass null.
- * Second Argument: On return this will have the
- * UserID (UID) of the new user. We pass in a uid_t variable
- * so we can get the return value. This value isn't used but
- * is included for demonstration purposes.
- * Third Argument: On return this will have the
- * GroupID (GID) of the new user. We pass in a gid_t variable
- * so we can get the return value. This value isn't used but
- * is included for demonstration purposes.
- * Return Value: The console username expressed as a CFString.
- * This string must be eventually released with a CFRelease call.
- */
-
- consoleUserName = SCDynamicStoreCopyConsoleUser(NULL, &uid, &gid);
- return(consoleUserName);
- }
-
- /* ConsoleUserIsLoggedIn
- *
- * This function will return true if a console user is
- * currently logged into the system, and false otherwise.
- */
- Boolean ConsoleUserIsLoggedIn()
- {
- CFStringRef userName = CopyCurrentConsoleUsername();
- if (userName == NULL)
- {
- //username is NULL thus no user is logged in
- return(FALSE);
- }
- else
- {
- //username is non-NULL thus a console user is present
- CFRelease(userName); //release username
- return(TRUE);
- }
- }
-
- int main()
- {
- Boolean result;
-
- result = ConsoleUserIsLoggedIn();
-
- if (result == TRUE)
- {
- printf("A console user is currently logged in\n");
-
- //Getting and printing the current username.
- printf("Current User: \t"); fflush(stdout);
- CFShow(CopyCurrentConsoleUsername());
- }
- else
- {
- printf("No user is currently logged in");
- }
-
- return(0);
- }
-